<link rel="stylesheet" href="../assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="../assets/css/scrollbar.css">
<form method="POST" class="form-horizontal">
    <div class="form-group">
        <label for="name">商品名称</label>
        <input name="name" required type="text" class="form-control" id="name" placeholder="商品名称">
    </div>
    <div class="form-group">
        <label for="cover">商品封面</label>
        <input name="cover" type="text"  class="form-control"  id="cover" placeholder="商品封面"/>
    </div>
    <div class="form-group">
        <label for="price">零售价格</label>
        <input name="price" required type="number" class="form-control" id="price" placeholder="商品名称">
    </div>
    <div class="form-group">
        <label for="market-price">市场价格</label>
        <input name="market_price" required type="number" class="form-control" id="market-price" placeholder="商品名称">
    </div>
    <div class="form-group">
        <label for="sales-volume">累计销量</label>
        <input type="text" required name="sales_volume" id="sales-volume" class="form-control">

    </div>

    <div class="form-group">
        <label for="description">商品描述</label>
        <input name="description" required type="text"  class="form-control" id="description" placeholder="商品描述">
    </div>
    <div class="form-group">
        <label for="brand">品牌</label>
        <select name="brand" required  id="brand" class="form-control">
            <!-- <option value="">-- 请选择 --</option> -->
            <option value="苹果">苹果</option>
            <option value="三星">三星</option>
            <option value="华为">华为</option>
            <option value="小米">小米</option>
            <option value="格力">格力</option>
            <option value="锤子">锤子</option>
            <option value="联想">联想</option>
            <option value="OPPO">OPPO</option>
            <option value="VIVO">VIVO</option>

        </select>
    </div>
    <div class="form-group">
        <label for="category">分类</label>
        <select name="category" required  id="category" class="form-control">
            <!-- <option value="">-- 请选择 --</option> -->
            <option value="智能手机">智能手机</option>
            <option value="数码家电">数码家电</option>
            <option value="笔记本">笔记本</option>
            <option value="智能设备">智能设备</option>
            <option value="生活用品">生活用品</option>
            <option value="生鲜水果">生鲜水果</option>
            <option value="母婴用品">母婴用品</option>
            <option value="汽车用品">汽车用品</option>
            <option value="美妆用品">美妆用品</option>
        </select>
    </div>
    <div class="form-group">
        <label for="introduce">商品详情</label>
        <script id="introduce" type="text/plain" style="width:100%;height:300px;"></script>
    </div>
    <button type="submit" class="btn btn-lg btn-block btn-primary">立即提交</button>
</form>
<script src="../assets/js/jquery.js"></script>
<script src="../assets/bootstrap/js/bootstrap.min.js"></script>
<!-- ueditor -->
<script type="text/javascript" charset="utf-8" src="../plugins/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="../plugins/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="../plugins/ueditor/lang/zh-cn/zh-cn.js"></script>
<!-- ueditor -->

<script>
    $(function () {
        // 实例化ueditor
        var ue = UE.getEditor('introduce');
        // 查询当前商品的信息
        var id=parseInt(location.search.substr(1).split('=')[1]);
        if(id<=0){
            alert('参数错误');
            location.href="./product-list.html";
        }
        $.get(`../apis/index.php?c=Product&a=detail`,{id},function(res){
            if(res.errcode===0){
                console.log(res);
                var {data}=res;
                $("input[name='name']").val(data.name);
                $("input[name='cover']").val(data.cover);
                $("input[name='price']").val(data.price);
                $("input[name='market_price']").val(data.market_price);
                $("input[name='sales_volume']").val(data.sales_volume);
                $("input[name='description']").val(data.description);
                // $("#category option").props();
                $("#category").find("option[value='"+data.category+"']").attr("selected",true);
                $("#brand").find("option[value='"+data.brand+"']").attr("selected",true);
                // 设置富文本的内容
                UE.getEditor('introduce').setContent(data.introduce);
            }
        },'json');

        // 提交商品最新信息
        $("form").on('submit',function(e){
            e.preventDefault();
            var postData=$("form").serialize();
            // var richText=ue.getContent();
            // console.log(postData);
            $.post(`../apis/index.php?c=Product&a=edit&id=${id}`,postData,function(res){
                console.log(res);
                if(res.errcode==0){
                    alert('编辑成功');
                }
            },'json');
        })
    });
</script>